mui.init({
	beforeback: function() {
		//获得列表界面的webview  
		var list = plus.webview.currentWebview().opener();
		//触发列表界面的自定义事件（refresh）,从而进行数据刷新  
		mui.fire(list, 'refresh');
		//返回true，继续页面关闭逻辑  
		return true;
	}
}); //页面初始化
/**
 * 请求用户详情接口
 */
//请求方法
function my_infomation() {
	interfRequest.user_info.data = {
		refresh: "1"
	}
	socialRequest(interfRequest.user_info)
}

//成功函数回调
function user_infoHandle(data) { 
	var head_img_url = data.data.head_img_url;
	if(!head_img_url){
		$('#my_heard').attr('src', '../../images/log.png')
	} else{
		$('#my_heard').attr('src', 'https://ypao.oss-cn-shenzhen.aliyuncs.com/' + data.data.head_img_url)
	}
	$('#mylisy_name').text(data.data.nickname); //昵称
	$('#mylist_bottom').text(data.data.personality_signature); //个性签名
	var sex = data.data.sex;
	if(sex == 1) {
		$("#gender_btn").text('男')
	} else if(sex == 2) {
		$("#gender_btn").text('女')
	} 
}

/**
 * 图片上传修改接口
 */
function editUserInfoHandle(data) {
	mui.toast("修改成功！"); 
	$("#my_heard").attr('src', data.headImgUrl); //头像给值
}

/**
 * 性别修改接口
 */
function amendUserInfoHandle(data) {
	mui.toast("修改成功！");
}

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		my_infomation();
		//刷新界面
		window.addEventListener('refresh', function(e) { //执行刷新
			location.reload();
		});

	});
} 

//修改性别
$(document).on('tap', '.leaber_num', function() {
	var leaber_num = $(this).attr('leaber_num'); 
	if(leaber_num == 1) {
		$("#gender_btn").text('男')
	} else if(leaber_num == 2) {
		$("#gender_btn").text('女')
	}
	interfRequest.amendUserInfo.data = {
		type: '4',
		update: leaber_num
	}
	socialRequest(interfRequest.amendUserInfo)
})

//昵称
mui('body').on('tap', '#name_nickname', function() {
	window.top.location.href = "../my/nickname.html"
});

//个性签名
mui('body').on('tap', '#signature_btn', function() {
	window.top.location.href = "../my/personalized_signature.html"
});


//点击上传图片
$(document).on('tap', '#head_portrait', function() {
	a = $(this);
	getImg(a);
});

//点击事件，弹出选择摄像头和相册的选项
function getImg(a) {
	var bts = [{
		title: "拍照"
	}, {
		title: "从相册选择"
	}];
	plus.nativeUI.actionSheet({
			cancel: "取消",
			buttons: bts
		},
		function(e) {
			if(e.index == 1) {
				getImage(a);
			} else if(e.index == 2) {
				galleryImgs(a);
			}
		}
	);
}

//调用手机摄像头并拍照
function getImage(a) {
	var cmr = plus.camera.getCamera();
	cmr.captureImage(function(p) {
		// alert('拍照'+p)
		plus.io.resolveLocalFileSystemURL(p, function(entry) {
			compressImage(entry.toLocalURL(), entry.name);
			var img = a.find("#my_heard"); //图片位置
			img.attr("src", entry.toLocalURL());
		}, function(e) {
			plus.nativeUI.toast("读取拍照文件错误：" + e.message);
		});
	}, function(e) {}, {
		filter: 'image'
	});
}

//从相册选择照片
function galleryImgs(a) {
	plus.gallery.pick(function(e) {
		// alert(e)
		var name = e.substr(e.lastIndexOf('/') + 1);
		compressImage(e, name);
		var img = a.find("#my_heard"); //图片位置
		img.attr("src", e);
	}, function(e) {}, {
		filter: "image"
	});
}

//压缩图片
function compressImage(url, filename) {
	var name = "_doc/upload/" + filename;
	plus.zip.compressImage({
			src: url, //src: (String 类型 )压缩转换原始图片的路径
			dst: name, //压缩转换目标图片的路径
			quality: 30, //quality: (Number 类型 )压缩图片的质量.取值范围为1-100
			overwrite: true //overwrite: (Boolean 类型 )覆盖生成新文件
		},
		function(zip) {
			createUpload(zip.target)
		},
		function(error) {
			plus.nativeUI.toast("压缩图片失败，请稍候再试");
		});
}

//上传图片
function createUpload(url) {
	//服务器地址
	var server = "http://39.98.247.30/member-client/api/open/upload";
	var wt = plus.nativeUI.showWaiting();
	var task = plus.uploader.createUpload(server, {
			method: "POST"
		},
		function(t, status) { //上传完成
			if(status == 200) {
				//mui.toast("上传成功");
				var headimg = JSON.parse(t.responseText)
				var headImgUrl = headimg.data;
				interfRequest.editUserInfo.data = {
					type: '1',
					update: headImgUrl
				}
				socialRequest(interfRequest.editUserInfo)
				//a.attr('img_url', headImgUrl);
				wt.close(); //关闭等待提示按钮
			} else {
				mui.toast("上传失败：" + status);
				wt.close(); //关闭等待提示按钮
			}
		}
	);
	//将文件添加到上传队列中
	task.addFile(url, {
		key: 'file'
	});
	task.start();
}